<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="账号名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ERP订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入ERP订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payWay">
        <el-input
          v-model="queryParams.payWay"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="payTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>

        <!--        <el-date-picker clearable-->
        <!--                        v-model="queryParams.payTime"-->
        <!--                        type="date"-->
        <!--                        value-format="yyyy-MM-dd"-->
        <!--                        placeholder="请选择支付时间">-->
        <!--        </el-date-picker>-->
      </el-form-item>
      <el-form-item label="阿里订单号" prop="ailOrderNumber">
        <el-input
          v-model="queryParams.ailOrderNumber"
          placeholder="请输入阿里订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信订单号" prop="vxOrderNumber">
        <el-input
          v-model="queryParams.vxOrderNumber"
          placeholder="请输入微信订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysServiceMarketOrderList" @selection-change="handleSelectionChange" border
              stripe>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="账号id" align="center" prop="userId"/>
      <el-table-column label="店铺id" align="center" prop="storeId"/>
      <el-table-column label="ERP订单号" align="center" prop="orderNumber"/>
      <el-table-column label="服务id" align="center" prop="serviceId"/>
      <el-table-column label="总价" align="center" prop="price"/>
      <el-table-column label="订单状态" align="center" prop="status"/>
      <el-table-column label="支付方式" align="center" prop="payWay"/>
      <el-table-column label="支付时间" align="center" prop="payTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阿里订单号" align="center" prop="ailOrderNumber"/>
      <el-table-column label="微信订单号" align="center" prop="vxOrderNumber"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务市场订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入账号id"/>
        </el-form-item>
        <el-form-item label="店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺id"/>
        </el-form-item>
        <el-form-item label="ERP订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入ERP订单号"/>
        </el-form-item>
        <el-form-item label="服务id" prop="serviceId">
          <el-input v-model="form.serviceId" placeholder="请输入服务id"/>
        </el-form-item>
        <el-form-item label="总价" prop="price">
          <el-input v-model="form.price" placeholder="请输入总价"/>
        </el-form-item>
        <el-form-item label="支付方式" prop="payWay">
          <el-input v-model="form.payWay" placeholder="请输入支付方式"/>
        </el-form-item>
        <el-form-item label="阿里订单号" prop="ailOrderNumber">
          <el-input v-model="form.ailOrderNumber" placeholder="请输入阿里订单号"/>
        </el-form-item>
        <el-form-item label="微信订单号" prop="vxOrderNumber">
          <el-input v-model="form.vxOrderNumber" placeholder="请输入微信订单号"/>
        </el-form-item>
        <el-form-item label="" prop="extend1">
          <el-input v-model="form.extend1" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="" prop="extend2">
          <el-input v-model="form.extend2" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="" prop="extend3">
          <el-input v-model="form.extend3" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="乐观锁" prop="revision">
          <el-input v-model="form.revision" placeholder="请输入乐观锁"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSysServiceMarketOrder,
  getSysServiceMarketOrder,
  delSysServiceMarketOrder,
  addSysServiceMarketOrder,
  updateSysServiceMarketOrder
} from "@/api/services/sysServiceMarketOrder";
import {formatDate} from "@/utils/index"

export default {
  name: "SysServiceMarketOrder",
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务市场订单表格数据
      sysServiceMarketOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      payTime: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        storeId: undefined,
        orderNumber: undefined,
        serviceId: undefined,
        price: undefined,
        status: undefined,
        payWay: undefined,
        ailOrderNumber: undefined,
        vxOrderNumber: undefined,
        extend1: undefined,
        extend2: undefined,
        extend3: undefined,
        revision: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "主键不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "账号id不能为空", trigger: "blur"}
        ],
        storeId: [
          {required: true, message: "店铺id不能为空", trigger: "blur"}
        ],
        orderNumber: [
          {required: true, message: "ERP订单号不能为空", trigger: "blur"}
        ],
        serviceId: [
          {required: true, message: "服务id不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "总价不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "订单状态不能为空", trigger: "change"}
        ],
        payWay: [
          {required: true, message: "支付方式不能为空", trigger: "blur"}
        ],
        ailOrderNumber: [
          {required: true, message: "阿里订单号不能为空", trigger: "blur"}
        ],
        vxOrderNumber: [
          {required: true, message: "微信订单号不能为空", trigger: "blur"}
        ],
        extend1: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        extend2: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        extend3: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        revision: [
          {required: true, message: "乐观锁不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务市场订单列表 */
    getList() {
      this.loading = true;
      if (this.payTime) {
        this.queryParams.beginPayTime = formatDate(new Date(this.payTime[0]))
        this.queryParams.endPayTime = formatDate(new Date(this.payTime[1]))
      }
      console.log('this.queryParams', this.queryParams)
      listSysServiceMarketOrder(this.queryParams).then(response => {
        this.sysServiceMarketOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        storeId: undefined,
        orderNumber: undefined,
        serviceId: undefined,
        price: undefined,
        status: undefined,
        payWay: undefined,
        payTime: undefined,
        ailOrderNumber: undefined,
        vxOrderNumber: undefined,
        extend1: undefined,
        extend2: undefined,
        extend3: undefined,
        revision: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务市场订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSysServiceMarketOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改服务市场订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSysServiceMarketOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysServiceMarketOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务市场订单编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSysServiceMarketOrder(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysServiceMarketOrder/export', {
        ...this.queryParams
      }, `sysServiceMarketOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
